<template>
	<view style="background-color: #EFEFEF;height: 100vh;overflow: auto;">
		<u-toast ref="uToast" />
		<view class="coupons_box">
			
			<navigator v-for="(v,k) in data" :key="k" :url="'/pages/coupons_details/coupons_details?id='+v.c_id" hover-class="navigator-hover">
			<view class="coupons_item">
				
				<view class="item_left">
					<view class="money">{{ v.c_name }}</view>
					<view class="describe">.凭券可享受减{{ v.c_money }}元优惠</view>
					<view class="describe">.满{{ v.c_above_money }}元可用</view>
					<view class="describe">.有效期至{{ $u.timeFormat(v.c_endTime, 'yyyy年mm月dd日 hh:MM') }}</view>
					<view class="describe">.可与其他优惠活动同享</view>
				</view>
				
				<view class="item_right">
					<view class="butt">去领取</view>
				</view>
				
			</view>
			</navigator>
			
		</view>
		
		<view v-if="loading" class="tips_messgae">
			加载中...
		</view>
		<view @click="getData()" v-else-if="error" class="tips_messgae">
			加载失败，点我重试
		</view>
		<view v-else-if="end" class="tips_messgae">
			我也是有底线的
		</view>
		<view v-else-if="data.length<=0" class="tips_messgae">暂无数据~</view>
		
	</view>
</template>

<script>
	export default {
		data(){
			return {
				data:[],
				page:1,
				end:false,
				loading:false,
				error:false
			}
		},
		methods: {
			getData(){
				let that = this
				
				this.loading = true
				
				this.$request({
					url:'/coupons',
					data:{page:that.page},
					method:'post',
					success(res){
						if(res.statusCode==200){
							
							if(res.data.code==1){
								
								for(var i in res.data.data.data){
									that.data.push(res.data.data.data[i])
								}
								
								if(res.data.data.page>=res.data.data.allpage){
									that.end = true
								}else{
									that.page = res.data.data.page+1
								}
								
							}else{
								that.$refs.uToast.show({
									title: res.data.msg,
									type: 'warning'
								})
								
								that.error = true
							}
							
						}else{
							that.$refs.uToast.show({
								title: '网络繁忙',
								type: 'warning'
							})
							
							that.error = true
						}
					},
					fail(err){
						that.$refs.uToast.show({
							title: '获取失败~',
							type: 'warning'
						})
						
						that.error = true
					},
					complete(){
						that.loading = false
						
						uni.stopPullDownRefresh()
					}
				})
			}
		},
		onLoad(){
			this.getData()
		},
		onPullDownRefresh(){
			this.page=1
			this.end = false
			this.data = []
			this.getData()
		},
		onReachBottom(){
			if(this.end){
				return false
			}
			
			this.page=this.page+1
			
			this.getData();
		}
	}
</script>

<style scoped>
	view.coupons_box{
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	view.coupons_box view.coupons_item{
		width:670rpx;
		padding: 20rpx;
		min-height: 220rpx;
		background-color: #fff;
		border-radius: 15rpx;
		margin-top: 20rpx;
		display: flex;
		font-size: 24rpx;
		justify-content: space-between;
	}
	view.coupons_box view.coupons_item view.item_left view.money{
		max-width: 360rpx;
		font-size: 34rpx;
		font-weight: bold;
		margin-bottom: 30rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}
	view.coupons_box view.coupons_item view.item_left view.describe{
		color:#888888;
		margin-top: 5rpx;
	}
	view.coupons_box view.coupons_item view.item_right .money{
		color:#EA4389;
		font-size: 30rpx;
		/* font-weight: bold; */
	}
	view.coupons_box view.coupons_item view.item_right {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	view.coupons_box view.coupons_item view.item_right .butt{
		padding: 10rpx 30rpx;
		background-color: #EA4389;
		color:#fff;
		border-radius: 30rpx;
		margin-top: 15rpx;
	}
</style>
